package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketAdminExample;
import com.cskaoyan.market.db.mapper.MarketAdminMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author lixianbiao
 * @since 2024/06/28 19:18
 */
@Service
public class AdminAdminServiceImpl implements AdminAdminService{
    @Autowired
    private MarketAdminMapper adminMapper;
    @Override
    public List<MarketAdmin> list(int page, int limit, String sort, String order, String username) {
        MarketAdminExample adminExample = new MarketAdminExample();
        adminExample.setOrderByClause(sort+" "+order);
        MarketAdminExample.Criteria criteria = adminExample.createCriteria();
        if(!StringUtils.isEmpty(username)){
            criteria.andUsernameLike("%"+username+"%");
        }
        PageHelper.startPage(page,limit);
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(adminExample);
        return marketAdmins;
    }

    @Override
    @Transactional
    public void create(MarketAdmin admin) {
        LocalDateTime now = LocalDateTime.now();
        admin.setAddTime(now);
        admin.setUpdateTime(now);
        admin.setDeleted(false);
        adminMapper.insertSelective(admin);
    }
}